<template>
  <div class="alloc-role">
    <i-dialog
      title="分配角色"
      width="1174px"
      height="448px"
      :visible="visible"
      :close-on-click-modal="false"
      @close="outerClose"
    >
      <TableSearch
        ref="tableSearch"
        :formItemsInline="formItemsInline"
        :columns="columns"
        :tableData="tableData"
        :form="form"
        :page-name-display="false"
        @handleTableData="handleTableData"
        @reset="reset"
      >
        <el-table-column width="200" slot="radio">
          <template #default="{ row }">
            <el-radio
              v-model="checkedRoleId"
              :label="row.id"
              @change="handleChange(row.id)"
              >{{ '' }}</el-radio
            >
          </template>
        </el-table-column>
      </TableSearch>
      <span slot="footer" class="dialog-footer">
        <el-button @click="outerClose">取 消</el-button>
        <el-button type="primary" @click="submit">保存</el-button>
      </span>
    </i-dialog>
  </div>
</template>
<script>
import TableSearch from '@/components/TableSearch'
import { findRoles, modifyUserRole } from '@/api/permission'
import formatter from '@/utils/format'
export default {
  components: { TableSearch },
  data() {
    return {
      form: {
        showName: '',
      },
      checkedRoleId: '',
      formItemsInline: [
        {
          type: 'text',
          value: 'showName',
          placeholder: '请输入角色名称进行查询',
          clearable: true,
        },
      ],
      columns: [
        {
          slot: 'radio',
        },
        {
          label: '角色名称',
          prop: 'showName',
        },
        {
          label: '角色说明',
          prop: 'explanation',
        },
        {
          label: '用户类型',
          prop: 'userType',
          formatter,
        },
        {
          label: '用户状态',
          prop: 'enable',
          // ifRender: (row) => {
          //   return row.enable
          // },
          formatter: (row) => {
            return row.enable ? '启用' : '禁用'
          },
        },
      ],
      tableData: {},
    }
  },
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    userAttr: {
      type: Object,
      default: () => {},
    },
  },
  computed: {
    resourceSet() {
      return this.$store.getters.resourceSet
    },
  },
  created() {
    this.checkedRoleId = this.userAttr.roleId
  },
  methods: {
    reset() {
      this.form = {
        showName: '',
      }
    },
    outerClose() {
      this.form = {
        showName: '',
      }
      this.$emit('update:visible', false)
    },
    handleTableData(params) {
      params.systemUserType = this.userAttr.type
      findRoles(params).then((res) => {
        this.tableData = {
          list: res.list.filter((item) => {
            return item.enable
          }),
          totalCount: res.totalAmount,
        }
      })
    },
    refreshTable() {
      this.$emit('refreshTable')
    },
    handleChange(id) {
      this.checkedRoleId = id
    },
    submit() {
      let params = {
        roleId: this.checkedRoleId,
        userId: this.userAttr.id,
      }
      modifyUserRole(params).then(
        () => {
          this.$message({
            message: '用户角色分配成功',
            type: 'success',
          })
          this.refreshTable()
          this.outerClose()
        },
        () => {
          this.refreshTable()
          this.outerClose()
        }
      )
    },
  },
}
</script>
<style lang="scss" scoped>
.alloc-role {
  &::v-deep .table-wrapper {
    height: 500px;
    overflow: auto;
  }
}
</style>
